#include "random.h"

#ifndef TEST_I2C_BUFFER_SIZE
#define TEST_I2C_BUFFER_SIZE (I2C_BUFFER_LENGTH)
#else

hal_i2c_config_t USE_ACQUIRE_WIRE_BUFFER() {
#if TEST_I2C_BUFFER_SIZE != 0
    hal_i2c_config_t config = {
		.size = sizeof(hal_i2c_config_t),
		.version = HAL_I2C_CONFIG_VERSION_1,
		.rx_buffer = new (std::nothrow) uint8_t[TEST_I2C_BUFFER_SIZE],
		.rx_buffer_size = TEST_I2C_BUFFER_SIZE,
		.tx_buffer = new (std::nothrow) uint8_t[TEST_I2C_BUFFER_SIZE],
		.tx_buffer_size = TEST_I2C_BUFFER_SIZE
	};
#else
# undef TEST_I2C_BUFFER_SIZE
# define TEST_I2C_BUFFER_SIZE (I2C_BUFFER_LENGTH)
    // Compatibility mode, where the buffers should be generated by HAL
    hal_i2c_config_t config = {
        .size = sizeof(hal_i2c_config_t),
        .version = HAL_I2C_CONFIG_VERSION_1
    };
#endif // TEST_I2C_BUFFER_SIZE != 0

	return config;
}

#endif // TEST_I2C_BUFFER_SIZE

#define TRANSFER_LENGTH_1 (TEST_I2C_BUFFER_SIZE)
#define TRANSFER_LENGTH_2 (TEST_I2C_BUFFER_SIZE)

#define BASE_SEED (20200101)
#define MASTER_TEST_MESSAGE_SEED (BASE_SEED + 1)
#define SLAVE_TEST_MESSAGE_SEED (BASE_SEED + 2)

#define I2C_DELAY 0
#define I2C_ADDRESS 0x32

static uint8_t I2C_Test_Tx_Buffer[TEST_I2C_BUFFER_SIZE + 1];
static uint8_t I2C_Test_Rx_Buffer[TEST_I2C_BUFFER_SIZE + 1];
static char MASTER_TEST_MESSAGE[TEST_I2C_BUFFER_SIZE - sizeof(uint32_t)] = {};
static char SLAVE_TEST_MESSAGE[TEST_I2C_BUFFER_SIZE] = {};

STARTUP({
    Random rMaster(MASTER_TEST_MESSAGE_SEED);
    Random rSlave(SLAVE_TEST_MESSAGE_SEED);

    rMaster.genBase32(MASTER_TEST_MESSAGE, sizeof(MASTER_TEST_MESSAGE) - 1);
    rSlave.genBase32(SLAVE_TEST_MESSAGE, sizeof(SLAVE_TEST_MESSAGE) - 1);
});
